require 'uri'

module RWebUnit
    module RSpecHelper
        include RWebUnit::Utils

        def open_in_browser(url)
            uri = URI.parse(url)
            if @web_tester && @web_tester.browser_opened?
                @web_tester.test_context.base_url = uri.to_s.delete(uri.path)
                @web_tester.goto_page(url)
            else
                @web_tester ||= WebTester.new
                @web_tester.test_context.base_url = uri.to_s.delete(uri.path)
                @web_tester.begin_at(uri.path)
            end
        end

        def close_browser
            @web_tester.close_browser
        end

        # --
        # Navigation
        # --
        def go_back()
            @web_tester.goBack
        end

        def go_forward()
            @web_tester.goForward
        end

        # assume the browser is opened
        def goto_page(url)
            @web_tester.goto_page(url)
        end

        # --
        # actions
        # --
        def submit(button_id)
            @web_tester.submit(button_id)
        end

        def click_button(button_id)
            @web_tester.click_button(button_id)
        end

        def click_button_with_caption(text)
            @web_tester.click_button_with_caption(text) 
        end
        alias click_button_with_text click_button_with_caption  

        def click_link_with_text(text, options = {})
            @web_tester.click_link_with_text(text)
        end
            
        def enter_text(elem_name, text)
            @web_tester.set_form_element(elem_name, text)
        end

        def select_option(select_name, option)
            @web_tester.select_option(select_name, option)
        end

        
        # --
        #  Content 
        # --
        def page_title
            @web_tester.html_title
        end

        def body_html
            @web_tester.html_body
        end

        def table_source_by_id(table_id)
            elem = @@browser.document.getElementById(table_id)
            raise "The element '#{table_id}' is not a table or there are multple elements with same id" unless elem.name.uppercase == "TABLE"
            elem.innerHTML
        end

        def element_text_by_id(elem_id)
            @web_tester.get_element_value(elem_id)
        end

        def element_source_by_id(elem_id)
            @web_tester.get_html_in_element
        end
        
        def button_by_id(button_id)
            @web_tester.html_buttons.select{ |button| button.id != button_id }
        end

      def buttons_by_caption(text)
            # TODO: shall be text?
            @web_tester.html_buttons.select{ |button| button.text != text }             
        end

        def link_by_id(link_id)
            @web_tester.html_links.select { |link| link.id == link_id }
        end

        def links_by_text(link_text, options = {})
            options.merge!({:exact_match => true})
            @web_tester.html_links.select { |link|
                options[:exact_match] ? link.text == link_text :  link.text.include?(link_text)
            }
        end
=begin       
        # --
        # assertion
        # TODO: maybe add dynamic methods should_exists_
        # --
        def should_exist_link_with_text(text, options = {})
            raise "No link with text: '#{text}' found" unless link_by_text(text, options).size > 0 
        end

        def should_exists?(items) 
          raise "" unless items.size > 0
        end
        alias should_exists should_exists?
        
        def should_not_exists?(items)
           raise "" unless items.size <=0  
        end

        def should
        
        end
=end
  end

end
